<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/Home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="T1">
      <el-alert title="注意：只允许为三级分类提供相应的参数" type="warning" show-icon class="T1" :closable="false"></el-alert>
      <el-row>
        <el-col :span="10">
          <span>请选择商品分类：</span>
          <el-cascader
            class="T1 cascader"
            v-model="value"
            :options="options"
            :props="{ expandTrigger: 'hover' }"
            @change="handleChange"
          ></el-cascader>
        </el-col>
      </el-row>
      <el-tabs v-model="activeName" @tab-click="handleClick" class="T1">
        <el-tab-pane label="动态参数" name="many">
          <el-button type="primary" :disabled="isBtnDis" @click="dialogVisible=true">添加参数</el-button>
          <el-table :data="manyTable" border stripe class="T1">
            <el-table-column label="*" type="expand">
              <slot slot-scope="scope">
                <el-tag
                  v-for="(item,index) in scope.row.strVal.split(' ') "
                  :key="index"
                  class="M1"
                  closable
                  v-show="item.length>0"
                  @close="deleteTag(scope.row,item)"
                >{{item}}</el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="addTag(scope.row)"
                />
                <el-button v-else size="small" @click="showInput(scope.row)">+ New Tag</el-button>
              </slot>
            </el-table-column>
            <el-table-column label="*" type="index"></el-table-column>
            <el-table-column label="参数名称" prop="name"></el-table-column>
            <el-table-column label="操作">
              <slot slot-scope="scope">
                <el-button
                  size="mini"
                  type="primary"
                  icon="el-icon-edit"
                  @click="editUser(scope.row)"
                >编辑</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  icon="el-icon-delete"
                  @click="deleteUser(scope.row)"
                >删除</el-button>
              </slot>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
          <el-button type="primary" :disabled="isBtnDis" @click="dialogVisible=true">添加属性</el-button>
          <el-table :data="onlyTable" border stripe class="T1">
            <el-table-column label="*" type="expand">
              <slot slot-scope="scope">
                <el-tag
                  v-for="(item,index) in scope.row.strVal.split(' ') "
                  :key="index"
                  class="M1"
                  closable
                  v-show="item.length>0"
                  @close="deleteTag(scope.row,item)"
                >{{item}}</el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="inputValue"
                  ref="saveTagInput"
                  size="small"
                  focus
                  @keyup.enter.native="addTag(scope.row)"
                />
                <el-button v-else size="small" @click="showInput(scope.row)">+ New Tag</el-button>
              </slot>
            </el-table-column>
            <el-table-column label="*" type="index"></el-table-column>
            <el-table-column label="参数名称" prop="name"></el-table-column>
            <el-table-column label="操作">
              <slot slot-scope="scope">
                <el-button
                  size="mini"
                  type="primary"
                  icon="el-icon-edit"
                  @click="editUser(scope.row)"
                >编辑</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  icon="el-icon-delete"
                  @click="deleteUser(scope.row)"
                >删除</el-button>
              </slot>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>

    <el-dialog
      :title="'添加'+titleText"
      :visible.sync="dialogVisible"
      width="50%"
      @close="addDialogClose"
    >
      <el-form :model="ruleForm" status-icon :rules="rules" ref="addForm" label-width="100px">
        <el-form-item label="分类名称">
          <el-input type="text" v-model="ruleForm.name" prop="name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addParams">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      inputValue: "",
      value: [],
      options: [
        {
          value: "zhinan",
          label: "指南",
          children: [
            {
              value: "shejiyuanze",
              label: "设计原则",
              children: [
                {
                  value: "yizhi",
                  label: "一致"
                },
                {
                  value: "fankui",
                  label: "反馈"
                },
                {
                  value: "xiaolv",
                  label: "效率"
                },
                {
                  value: "kekong",
                  label: "可控"
                }
              ]
            },
            {
              value: "daohang",
              label: "导航",
              children: [
                {
                  value: "cexiangdaohang",
                  label: "侧向导航"
                },
                {
                  value: "dingbudaohang",
                  label: "顶部导航"
                }
              ]
            }
          ]
        },
        {
          value: "zujian",
          label: "组件",
          children: [
            {
              value: "basic",
              label: "Basic",
              children: [
                {
                  value: "layout",
                  label: "Layout 布局"
                },
                {
                  value: "color",
                  label: "Color 色彩"
                },
                {
                  value: "typography",
                  label: "Typography 字体"
                },
                {
                  value: "icon",
                  label: "Icon 图标"
                },
                {
                  value: "button",
                  label: "Button 按钮"
                }
              ]
            },
            {
              value: "form",
              label: "Form",
              children: [
                {
                  value: "radio",
                  label: "Radio 单选框"
                },
                {
                  value: "checkbox",
                  label: "Checkbox 多选框"
                },
                {
                  value: "input",
                  label: "Input 输入框"
                },
                {
                  value: "input-number",
                  label: "InputNumber 计数器"
                },
                {
                  value: "select",
                  label: "Select 选择器"
                },
                {
                  value: "cascader",
                  label: "Cascader 级联选择器"
                },
                {
                  value: "switch",
                  label: "Switch 开关"
                },
                {
                  value: "slider",
                  label: "Slider 滑块"
                },
                {
                  value: "time-picker",
                  label: "TimePicker 时间选择器"
                },
                {
                  value: "date-picker",
                  label: "DatePicker 日期选择器"
                },
                {
                  value: "datetime-picker",
                  label: "DateTimePicker 日期时间选择器"
                },
                {
                  value: "upload",
                  label: "Upload 上传"
                },
                {
                  value: "rate",
                  label: "Rate 评分"
                },
                {
                  value: "form",
                  label: "Form 表单"
                }
              ]
            },
            {
              value: "data",
              label: "Data",
              children: [
                {
                  value: "table",
                  label: "Table 表格"
                },
                {
                  value: "tag",
                  label: "Tag 标签"
                },
                {
                  value: "progress",
                  label: "Progress 进度条"
                },
                {
                  value: "tree",
                  label: "Tree 树形控件"
                },
                {
                  value: "pagination",
                  label: "Pagination 分页"
                },
                {
                  value: "badge",
                  label: "Badge 标记"
                }
              ]
            },
            {
              value: "notice",
              label: "Notice",
              children: [
                {
                  value: "alert",
                  label: "Alert 警告"
                },
                {
                  value: "loading",
                  label: "Loading 加载"
                },
                {
                  value: "message",
                  label: "Message 消息提示"
                },
                {
                  value: "message-box",
                  label: "MessageBox 弹框"
                },
                {
                  value: "notification",
                  label: "Notification 通知"
                }
              ]
            },
            {
              value: "navigation",
              label: "Navigation",
              children: [
                {
                  value: "menu",
                  label: "NavMenu 导航菜单"
                },
                {
                  value: "tabs",
                  label: "Tabs 标签页"
                },
                {
                  value: "breadcrumb",
                  label: "Breadcrumb 面包屑"
                },
                {
                  value: "dropdown",
                  label: "Dropdown 下拉菜单"
                },
                {
                  value: "steps",
                  label: "Steps 步骤条"
                }
              ]
            },
            {
              value: "others",
              label: "Others",
              children: [
                {
                  value: "dialog",
                  label: "Dialog 对话框"
                },
                {
                  value: "tooltip",
                  label: "Tooltip 文字提示"
                },
                {
                  value: "popover",
                  label: "Popover 弹出框"
                },
                {
                  value: "card",
                  label: "Card 卡片"
                },
                {
                  value: "carousel",
                  label: "Carousel 走马灯"
                },
                {
                  value: "collapse",
                  label: "Collapse 折叠面板"
                }
              ]
            }
          ]
        },
        {
          value: "ziyuan",
          label: "资源",
          children: [
            {
              value: "axure",
              label: "Axure Components"
            },
            {
              value: "sketch",
              label: "Sketch Templates"
            },
            {
              value: "jiaohu",
              label: "组件交互文档"
            }
          ]
        }
      ],
      activeName: "many",
      manyTable: [],
      onlyTable: [],
      dialogVisible: false,
      ruleForm: { name: "" },
      rules: {
        name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    handleChange() {
      if (this.value.length !== 3) {
        this.value = [];
        this.manyTable = [];
        this.onlyTable = [];
      } else {
        this.getList();
      }
    },
    handleClick() {
      this.getList();
    },
    getList(number) {
      if (this.activeName === "many") {
        this.manyTable = [
          {
            name: "x小胡",
            strVal:
              "常用于主动 操作后的反 提示 与 Notification 的区别是后 者更多用于 系统级 通知的被 动提醒。",
            inputVisible: false
          },
          {
            name: "x小胡",
            strVal: "常用于主动 操作后的反 提示 与 被 动提醒。",
            inputVisible: false
          }
        ];
      } else {
        this.onlyTable = [
          {
            name: "x胡",
            strVal:
              "常用于主 tion 的区别是后 者更多用于 系统级 通知的被 动提醒。",
            inputVisible: false
          },
          {
            name: "x胡",
            strVal:
              "常用于主动 操作后的反 提示 与 Notification 的区别是后 者更多用于 系统级 通知的被 动提醒。",
            inputVisible: false
          },
          {
            name: "x胡",
            strVal: "常用于主动 操作后的反 提示 与 Notification 的 醒。",
            inputVisible: false
          }
        ];
      }
    },
    addDialogClose() {
      this.$refs.addForm.resetFields();
    },
    addParams() {
      console.log(this.ruleForm);
      if (this.activeName === "many") {
        this.manyTable.push(this.ruleForm);
      } else {
        this.onlyTable.push(this.ruleForm);
      }
      this.$message({
        message: "添加成功，等待数据库恢复",
        type: "success",
        duration: 2000
      });
      this.dialogVisible = false;
    },
    deleteUser(data) {
      this.$message({
        message: "待开发，删除成功",
        type: "error",
        duration: 2000
      });
    },
    editUser(data) {
      this.$message({
        message: "待开发，修改成功",
        type: "success",
        duration: 2000
      });
    },
    addTag(data) {
      data.inputVisible = false;
      data.strVal += " " + this.inputValue;
      this.$message({
        message: "添加成功",
        type: "success",
        duration: 500
      });
      this.inputValue = "";
    },
    deleteTag(data, str) {
      data.strVal = data.strVal.replace(str, "");
      this.$message({
        message: "删除成功",
        type: "error",
        duration: 500
      });
    },
    showInput(data) {
      data.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    }
  },
  computed: {
    split(str) {
      return str.split(" ");
    },
    isBtnDis() {
      if (this.value.length !== 3) {
        return true;
      }
      return false;
    },
    titleText() {
      if (this.activeName === "many") {
        return "动态参数";
      } else {
        return "静态属性";
      }
    }
  }
};
</script>
<style scoped>
.M1 {
  margin: 10px;
}
.T1 {
  margin-top: 10px;
}
.cascader {
  width: 340px;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: center;
}
</style>